//
//  SumitPwdViewController.m
//  TengYunWaystation
//
//  Created by Johnny on 15/7/15.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "SumitPwdViewController.h"
#import "SurePwdViewController.h"
#import "SocketRequest.h"

@interface SumitPwdViewController ()<SocketRequestDelegate,UITextFieldDelegate> {
    SocketRequest *_socketRequest;
    NSString *_code;
}

@end

@implementation SumitPwdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"输入验证码";
        _socketRequest = [[SocketRequest alloc] initWithSocketRequestDelegate:self];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _inputView.layer.borderColor = LineColor.CGColor;
    _inputView.layer.borderWidth = 0.5;
    NSString *subPhone = [NSString stringWithFormat:@"%@****%@",[self.phone substringToIndex:3],[self.phone substringFromIndex:7]];
    _tipMessageLabel.text = [NSString stringWithFormat:@"我们已发送了验证码到号码：\n%@",subPhone];
    [_codeTextField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    [self getCodeSocket];
}

- (void)getCodeSocket {
    [ToastActivity makeActivity:@"获取验证码中..." InView:self.view];
    _countDownBtn.enabled = NO;
    NSMutableData *mutableData = [[NSMutableData alloc] initWithData:[SocketMethod socketHeadDataMethod:@"STU5"]];
    [mutableData appendBytes:[@"01" UTF8String] length:2];
    [mutableData appendBytes:[SocketMethod checkLegal:self.phone] length:16];
    [_socketRequest addSocketData:mutableData SocketTag:1];
}

- (void)textFieldEditingChanged:(UITextField *)textField {
    if (textField.text.length > 6) {
        textField.text = [textField.text substringToIndex:6];
        [textField resignFirstResponder];
        [DSToast showText:@"验证码不能超过6位" InView:self.view];
    }
}

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.text.length == 6) {
        SurePwdViewController *surePVC = [[SurePwdViewController alloc] initWithNibName:@"SurePwdViewController" bundle:nil];
        surePVC.phone = self.phone;
        surePVC.code = _codeTextField.text;
        [self.navigationController pushViewController:surePVC animated:YES];
    }
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:TelephoneLimit] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    if (![string isEqualToString:filtered]) {
        return NO;
    }
    return YES;
}

#pragma mark SocketRequestDelegate
- (void)socket:(GCDAsyncSocket *)socket Code:(NSString *)code Msg:(NSString *)msg Data:(NSData *)data {
    if ([code isEqualToString:SuccessSocket]) {
        _code = [SocketMethod stringData:data];
        [_countDownBtn startCountDown];
        [ToastActivity dismissInView:self.view];
//        [ToastActivity makeSuccess:@"获取成功" InView:self.view];
    } else {
        [ToastActivity makeError:msg InView:self.view];
    }
}

- (void)socket:(GCDAsyncSocket *)socket Msg:(NSString *)msg {
    [ToastActivity makeError:msg InView:self.view];
}


#pragma mark NibAction
- (IBAction)resetGetCode:(id)sender {
    [self getCodeSocket];
}

- (IBAction)sumitCodeAction:(id)sender {
    [_codeTextField resignFirstResponder];
    if (![_code isEqualToString:_codeTextField.text]) {
        [DSToast showText:@"验证码输入不正确" InView:self.view];
    } else {
        SurePwdViewController *surePVC = [[SurePwdViewController alloc] initWithNibName:@"SurePwdViewController" bundle:nil];
        surePVC.phone = self.phone;
        surePVC.code = _codeTextField.text;
        [self.navigationController pushViewController:surePVC animated:YES];
    }
}

- (void)dealloc {
    _socketRequest = nil;
}

@end
